package com.his.controller;

import com.his.annotation.RequireRoles;
import com.his.common.Const;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.Nursing;
import com.his.service.NursingService;
import com.his.utils.MyUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Nursing)表控制层
 *
 * @author makejava
 * @since 2020-10-23 18:13:29
 */
@RestController
@RequestMapping("nursing")
public class NursingController {
    /**
     * 服务对象
     */
    @Resource
    private NursingService nursingService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Nursing selectOne(Integer id) {
        return this.nursingService.queryById(id);
    }

    @RequireRoles(roles = {"nurse","admin"},index = 0)
    @PostMapping("selectAll")
    public Rsp selectAll(@RequestHeader(Const.TOKEN) String token, @RequestBody PageParam<Nursing> pageParam) throws Exception {
        return nursingService.queryAll(pageParam,MyUtil.getUser(token));
    }
    @RequireRoles(roles = {"nurse","admin"},index = 0)
    @PostMapping("add")
    public Rsp add(@RequestHeader(Const.TOKEN) String token, @Validated @RequestBody Nursing nursing) throws Exception {
        nursing.setNurseId(MyUtil.getUserByToken(token));
        return nursingService.insert(nursing) ? Rsp.success(null) : Rsp.fail().msg("添加失败");
    }
    @RequireRoles(roles = {"nurse","admin"},index = 0)
    @PostMapping("update")
    public Rsp update(@RequestHeader(Const.TOKEN) String token,@Validated({Const.Update.class}) @RequestBody Nursing nursing) {
        return nursingService.update(nursing)?Rsp.success(null) : Rsp.fail().msg("修改失败");
    }
}